using Infrastructure.Attribute;
using Infrastructure.Extensions;
using ZR.Model;
using ZR.Repository;
using ZR.Service.Business.IBusinessService;
using System.Linq;
using ZR.Model.Models.ProductionControl;
using ZR.Model.Dto.Business.ProductionControl;

namespace ZR.Service.Business
{
    /// <summary>
    /// 操作类型点位详情配置Service业务层处理
    /// </summary>
    [AppService(ServiceType = typeof(IOperationPointConfigService), ServiceLifetime = LifeTime.Transient)]
    public class OperationPointConfigService : BaseService<OperationPointConfig>, IOperationPointConfigService
    {
        /// <summary>
        /// 查询操作类型点位详情配置列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        public PagedInfo<OperationPointConfigDto> GetList(OperationPointConfigQueryDto parm)
        {
            var predicate = QueryExp(parm);

            var response = Queryable()
                .Where(predicate.ToExpression())
                .ToPage<OperationPointConfig, OperationPointConfigDto>(parm);

            return response;
        }

        /// <summary>
        /// 获取详情
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public OperationPointConfig GetInfo(int Id)
        {
            var response = Queryable()
                .Where(x => x.Id == Id)
                .First();

            return response;
        }

        public Dictionary<int, List<OperationPointConfig>> ListByOpAndType(string station, string line, string production, int pointtype, int itemNo)
        {
            return Queryable().Where(qbc =>
                        qbc.LineCode.Equals(line) &&
                        qbc.StationCode.Equals(station) &&
                        qbc.OperationType.Equals(pointtype) &&
                        //qbc.ProductionType.Equals(production) &&
                        qbc.PointId.Equals(itemNo)).ToList()
                        .GroupBy(ob => ob.ItemType)
                        .OrderBy(gb => gb.Key)
                        .ToDictionary(g => g.Key, g => g.ToList());
        }

        /// <summary>
        /// 添加操作类型点位详情配置
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public OperationPointConfig AddOperationPointConfig(OperationPointConfig model)
        {
            return Insertable(model).ExecuteReturnEntity();
        }

        /// <summary>
        /// 修改操作类型点位详情配置
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int UpdateOperationPointConfig(OperationPointConfig model)
        {
            return Update(model, true);
        }

        /// <summary>
        /// 查询导出表达式
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        private static Expressionable<OperationPointConfig> QueryExp(OperationPointConfigQueryDto parm)
        {
            var predicate = Expressionable.Create<OperationPointConfig>();

            return predicate;
        }
    }
}